package org.example.day07;

/**
 * 第三章节3.1.9: 方法wait()导致锁立即释放
 *      方法wait()导致锁立即释放，方法notify不会立即释放锁
 */
public class MyThread7 {
    public void testMethod(Object lock){
        try {
            synchronized (lock){
                System.out.println("begin wait()");
                lock.wait(); //释放锁
                System.out.println("end wait()");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Object o = new Object();
        Thread7A ta = new Thread7A(o);
        ta.start();
        Thread7B tb = new Thread7B(o);
        tb.start();
    }
}

class Thread7A extends Thread{
    private Object lock;

    public Thread7A(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        MyThread7 m7 = new MyThread7();
        m7.testMethod(lock);
    }
}

class Thread7B extends Thread{
    private Object lock;

    public Thread7B(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        MyThread7 m7 = new MyThread7();
        m7.testMethod(lock);
    }
}
